@@ -0,0 +1,214 @@ |
||
1 |
+module Agents |
|
2 |
+ class LiquidOutputAgent < Agent |
|
3 |
+ include WebRequestConcern |
|
4 |
+ include FormConfigurable |
|
5 |
+ |
|
6 |
+ cannot_be_scheduled! |
|
7 |
+ cannot_create_events! |
|
8 |
+ |
|
9 |
+ DATE_UNITS = %w[second seconds minute minutes hour hours day days week weeks month months year years] |
|
10 |
+ |
|
11 |
+ description do |
|
12 |
+ <<-MD |
|
13 |
+ The Liquid Output Agent outputs events through a Liquid template you provide. Use it to create a HTML page, or a json feed, or anything else that can be rendered as a string from your stream of Huginn data. |
|
14 |
+ |
|
15 |
+ This Agent will output data at: |
|
16 |
+ |
|
17 |
+ `https://#{ENV['DOMAIN']}#{Rails.application.routes.url_helpers.web_requests_path(agent_id: ':id', user_id: user_id, secret: ':secret', format: :any_extension)}` |
|
18 |
+ |
|
19 |
+ where `:secret` is the secret specified in your options. You can use any extension you wish. |
|
20 |
+ |
|
21 |
+ Options: |
|
22 |
+ |
|
23 |
+ * `secret` - A token that the requestor must provide for light-weight authentication. |
|
24 |
+ * `expected_receive_period_in_days` - How often you expect data to be received by this Agent from other Agents. |
|
25 |
+ * `content` - The content to display when someone requests this page. |
|
26 |
+ * `mime_type` - The mime type to use when someone requests this page. |
|
27 |
+ * `mode` - The behavior that determines what data is passed to the Liquid template. |
|
28 |
+ * `event_limit` - A limit applied to the events passed to a template when in "Last X events" mode. Can be a count like "1", or an amount of time like "1 day" or "5 minutes". |
|
29 |
+ |
|
30 |
+ # Liquid Templating |
|
31 |
+ |
|
32 |
+ The content you provide will be run as a Liquid template. The data from the last event received will be used when processing the Liquid template. |
|
33 |
+ |
|
34 |
+ To learn more about Liquid templates, go here: [http://liquidmarkup.org](http://liquidmarkup.org "Liquid Templating") |
|
35 |
+ |
|
36 |
+ # Modes |
|
37 |
+ |
|
38 |
+ ### Merge events |
|
39 |
+ |
|
40 |
+ The data for incoming events will be merged. So if two events come in like this: |
|
41 |
+ |
|
42 |
+``` |
|
43 |
+{ 'a' => 'b', 'c' => 'd'} |
|
44 |
+{ 'a' => 'bb', 'e' => 'f'} |
|
45 |
+``` |
|
46 |
+ |
|
47 |
+ The final result will be: |
|
48 |
+ |
|
49 |
+``` |
|
50 |
+{ 'a' => 'bb', 'c' => 'd', 'e' => 'f'} |
|
51 |
+``` |
|
52 |
+ |
|
53 |
+ This merged version will be passed to the Liquid template. |
|
54 |
+ |
|
55 |
+ ### Last event in |
|
56 |
+ |
|
57 |
+ The data from the last event will be passed to the template. |
|
58 |
+ |
|
59 |
+ ### Last X events |
|
60 |
+ |
|
61 |
+ All of the events received by this agent will be passed to the template |
|
62 |
+ as the ```events``` array. |
|
63 |
+ |
|
64 |
+ The number of events can be controlled via the ```event_limit``` option. |
|
65 |
+ If ```event_limit``` is an integer X, the last X events will be passed |
|
66 |
+ to the template. If ```event_limit``` is an integer with a unit of |
|
67 |
+ measure like "1 day" or "5 minutes" or "9 years", a date filter will |
|
68 |
+ be applied to the events passed to the template. If no ```event_limit``` |
|
69 |
+ is provided, then all of the events for the agent will be passed to |
|
70 |
+ the template. |
|
71 |
+ |
|
72 |
+ For performance, the maximum ```event_limit``` allowed is 1000. |
|
73 |
+ |
|
74 |
+ MD |
|
75 |
+ end |
|
76 |
+ |
|
77 |
+ def default_options |
|
78 |
+ content = <<EOF |
|
79 |
+When you use the "Last event in" or "Merge events" option, you can use variables from the last event received, like this: |
|
80 |
+ |
|
81 |
+Name: {{name}} |
|
82 |
+Url: {{url}} |
|
83 |
+ |
|
84 |
+If you use the "Last X Events" mode, a set of events will be passed to your Liquid template. You can use them like this: |
|
85 |
+ |
|
86 |
+<table class="table"> |
|
87 |
+ {% for event in events %} |
|
88 |
+ <tr> |
|
89 |
+ <td>{{ event.title }}</td> |
|
90 |
+ <td><a href="{{ event.url }}">Click here to see</a></td> |
|
91 |
+ </tr> |
|
92 |
+ {% endfor %} |
|
93 |
+</table> |
|
94 |
+EOF |
|
95 |
+ { |
|
96 |
+ "secret" => "a-secret-key", |
|
97 |
+ "expected_receive_period_in_days" => 2, |
|
98 |
+ "mime_type" => 'text/html', |
|
99 |
+ "mode" => 'Last event in', |
|
100 |
+ "event_limit" => '', |
|
101 |
+ "content" => content, |
|
102 |
+ } |
|
103 |
+ end |
|
104 |
+ |
|
105 |
+ form_configurable :secret |
|
106 |
+ form_configurable :expected_receive_period_in_days |
|
107 |
+ form_configurable :content, type: :text |
|
108 |
+ form_configurable :mime_type |
|
109 |
+ form_configurable :mode, type: :array, values: [ 'Last event in', 'Merge events', 'Last X events'] |
|
110 |
+ form_configurable :event_limit |
|
111 |
+ |
|
112 |
+ def working? |
|
113 |
+ last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs? |
|
114 |
+ end |
|
115 |
+ |
|
116 |
+ def validate_options |
|
117 |
+ if options['secret'].present? |
|
118 |
+ case options['secret'] |
|
119 |
+ when %r{[/.]} |
|
120 |
+ errors.add(:base, "secret may not contain a slash or dot") |
|
121 |
+ when String |
|
122 |
+ else |
|
123 |
+ errors.add(:base, "secret must be a string") |
|
124 |
+ end |
|
125 |
+ else |
|
126 |
+ errors.add(:base, "Please specify one secret for 'authenticating' incoming feed requests") |
|
127 |
+ end |
|
128 |
+ |
|
129 |
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0 |
|
130 |
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working") |
|
131 |
+ end |
|
132 |
+ |
|
133 |
+ if options['event_limit'].present? |
|
134 |
+ if((Integer(options['event_limit']) rescue false) == false) |
|
135 |
+ errors.add(:base, "Event limit must be an integer that is less than 1001.") |
|
136 |
+ elsif (options['event_limit'].to_i > 1000) |
|
137 |
+ errors.add(:base, "For performance reasons, you cannot have an event limit greater than 1000.") |
|
138 |
+ end |
|
139 |
+ else |
|
140 |
+ end |
|
141 |
+ end |
|
142 |
+ |
|
143 |
+ def receive(incoming_events) |
|
144 |
+ return unless ['merge events', 'last event in'].include?(mode) |
|
145 |
+ memory['last_event'] ||= {} |
|
146 |
+ incoming_events.each do |event| |
|
147 |
+ case mode |
|
148 |
+ when 'merge events' |
|
149 |
+ memory['last_event'] = memory['last_event'].merge(event.payload) |
|
150 |
+ else |
|
151 |
+ memory['last_event'] = event.payload |
|
152 |
+ end |
|
153 |
+ end |
|
154 |
+ end |
|
155 |
+ |
|
156 |
+ def receive_web_request(params, method, format) |
|
157 |
+ valid_authentication?(params) ? [liquified_content, 200, mime_type] |
|
158 |
+ : [unauthorized_content(format), 401] |
|
159 |
+ end |
|
160 |
+ |
|
161 |
+ private |
|
162 |
+ |
|
163 |
+ def mode |
|
164 |
+ options['mode'].to_s.downcase |
|
165 |
+ end |
|
166 |
+ |
|
167 |
+ def unauthorized_content(format) |
|
168 |
+ format =~ /json/ ? { error: "Not Authorized" } |
|
169 |
+ : "Not Authorized" |
|
170 |
+ end |
|
171 |
+ |
|
172 |
+ def valid_authentication?(params) |
|
173 |
+ interpolated['secret'] == params['secret'] |
|
174 |
+ end |
|
175 |
+ |
|
176 |
+ def mime_type |
|
177 |
+ options['mime_type'].presence || 'text/html' |
|
178 |
+ end |
|
179 |
+ |
|
180 |
+ def liquified_content |
|
181 |
+ template = Liquid::Template.parse(options['content'] || "") |
|
182 |
+ template.render(data_for_liquid_template) |
|
183 |
+ end |
|
184 |
+ |
|
185 |
+ def data_for_liquid_template |
|
186 |
+ case mode |
|
187 |
+ when 'last x events' |
|
188 |
+ events = received_events |
|
189 |
+ events = events.where('events.created_at > ?', date_limit) if date_limit |
|
190 |
+ events = events.limit count_limit |
|
191 |
+ events = events.to_a.map { |x| x.payload } |
|
192 |
+ { 'events' => events } |
|
193 |
+ else |
|
194 |
+ memory['last_event'] || {} |
|
195 |
+ end |
|
196 |
+ end |
|
197 |
+ |
|
198 |
+ def count_limit |
|
199 |
+ limit = Integer(options['event_limit']) rescue 1000 |
|
200 |
+ limit <= 1000 ? limit : 1000 |
|
201 |
+ end |
|
202 |
+ |
|
203 |
+ def date_limit |
|
204 |
+ return nil unless options['event_limit'].to_s.include?(' ') |
|
205 |
+ value, unit = options['event_limit'].split(' ') |
|
206 |
+ value = Integer(value) rescue nil |
|
207 |
+ return nil unless value |
|
208 |
+ unit = unit.to_s.downcase |
|
209 |
+ return nil unless DATE_UNITS.include?(unit) |
|
210 |
+ value.send(unit.to_sym).ago |
|
211 |
+ end |
|
212 |
+ |
|
213 |
+ end |
|
214 |
+end |
@@ -0,0 +1,15 @@ |
||
1 |
+<p> |
|
2 |
+ Data for this Agent is available at these URLs: |
|
3 |
+</p> |
|
4 |
+ |
|
5 |
+ |
|
6 |
+<ul> |
|
7 |
+ <% url = lambda { |format| web_requests_url(:agent_id => @agent.id, :user_id => current_user.id, :secret => @agent.options['secret'], :format => format) } %> |
|
8 |
+ <li><%= link_to url.call(:html), url.call(:html), :target => :blank %></li> |
|
9 |
+ <li><%= link_to url.call(:json), url.call(:json), :target => :blank %></li> |
|
10 |
+ <li><%= link_to url.call(:xml), url.call(:xml), :target => :blank %></li> |
|
11 |
+</ul> |
|
12 |
+ |
|
13 |
+<p> |
|
14 |
+ ... or any other extension you wish, as the extension does not change the content or mime type. |
|
15 |
+</p> |
@@ -0,0 +1,461 @@ |
||
1 |
+# encoding: utf-8 |
|
2 |
+ |
|
3 |
+require 'rails_helper' |
|
4 |
+ |
|
5 |
+describe Agents::LiquidOutputAgent do |
|
6 |
+ let(:agent) do |
|
7 |
+ _agent = Agents::LiquidOutputAgent.new(:name => 'My Data Output Agent') |
|
8 |
+ _agent.options = _agent.default_options.merge('secret' => 'secret1', 'events_to_show' => 3) |
|
9 |
+ _agent.options['secret'] = "a secret" |
|
10 |
+ _agent.user = users(:bob) |
|
11 |
+ _agent.sources << agents(:bob_website_agent) |
|
12 |
+ _agent.save! |
|
13 |
+ _agent |
|
14 |
+ end |
|
15 |
+ |
|
16 |
+ describe "#working?" do |
|
17 |
+ it "checks if events have been received within expected receive period" do |
|
18 |
+ expect(agent).not_to be_working |
|
19 |
+ Agents::LiquidOutputAgent.async_receive agent.id, [events(:bob_website_agent_event).id] |
|
20 |
+ expect(agent.reload).to be_working |
|
21 |
+ two_days_from_now = 2.days.from_now |
|
22 |
+ stub(Time).now { two_days_from_now } |
|
23 |
+ expect(agent.reload).not_to be_working |
|
24 |
+ end |
|
25 |
+ end |
|
26 |
+ |
|
27 |
+ describe "validation" do |
|
28 |
+ before do |
|
29 |
+ expect(agent).to be_valid |
|
30 |
+ end |
|
31 |
+ |
|
32 |
+ it "should validate presence and length of secret" do |
|
33 |
+ agent.options[:secret] = "" |
|
34 |
+ expect(agent).not_to be_valid |
|
35 |
+ agent.options[:secret] = "foo" |
|
36 |
+ expect(agent).to be_valid |
|
37 |
+ agent.options[:secret] = "foo/bar" |
|
38 |
+ expect(agent).not_to be_valid |
|
39 |
+ agent.options[:secret] = "foo.xml" |
|
40 |
+ expect(agent).not_to be_valid |
|
41 |
+ agent.options[:secret] = false |
|
42 |
+ expect(agent).not_to be_valid |
|
43 |
+ agent.options[:secret] = [] |
|
44 |
+ expect(agent).not_to be_valid |
|
45 |
+ agent.options[:secret] = ["foo.xml"] |
|
46 |
+ expect(agent).not_to be_valid |
|
47 |
+ agent.options[:secret] = ["hello", true] |
|
48 |
+ expect(agent).not_to be_valid |
|
49 |
+ agent.options[:secret] = ["hello"] |
|
50 |
+ expect(agent).not_to be_valid |
|
51 |
+ agent.options[:secret] = ["hello", "world"] |
|
52 |
+ expect(agent).not_to be_valid |
|
53 |
+ end |
|
54 |
+ |
|
55 |
+ it "should validate presence of expected_receive_period_in_days" do |
|
56 |
+ agent.options[:expected_receive_period_in_days] = "" |
|
57 |
+ expect(agent).not_to be_valid |
|
58 |
+ agent.options[:expected_receive_period_in_days] = 0 |
|
59 |
+ expect(agent).not_to be_valid |
|
60 |
+ agent.options[:expected_receive_period_in_days] = -1 |
|
61 |
+ expect(agent).not_to be_valid |
|
62 |
+ end |
|
63 |
+ |
|
64 |
+ it "should validate the event_limit" do |
|
65 |
+ agent.options[:event_limit] = "" |
|
66 |
+ expect(agent).to be_valid |
|
67 |
+ agent.options[:event_limit] = "1" |
|
68 |
+ expect(agent).to be_valid |
|
69 |
+ agent.options[:event_limit] = "1001" |
|
70 |
+ expect(agent).not_to be_valid |
|
71 |
+ agent.options[:event_limit] = "10000" |
|
72 |
+ expect(agent).not_to be_valid |
|
73 |
+ end |
|
74 |
+ |
|
75 |
+ it "should should not allow non-integer event limits" do |
|
76 |
+ agent.options[:event_limit] = "abc1234" |
|
77 |
+ expect(agent).not_to be_valid |
|
78 |
+ end |
|
79 |
+ end |
|
80 |
+ |
|
81 |
+ describe "#receive?" do |
|
82 |
+ |
|
83 |
+ let(:key) { SecureRandom.uuid } |
|
84 |
+ let(:value) { SecureRandom.uuid } |
|
85 |
+ |
|
86 |
+ let(:incoming_events) do |
|
87 |
+ last_payload = { key => value } |
|
88 |
+ [Struct.new(:payload).new( { key => SecureRandom.uuid } ), |
|
89 |
+ Struct.new(:payload).new( { key => SecureRandom.uuid } ), |
|
90 |
+ Struct.new(:payload).new(last_payload)] |
|
91 |
+ end |
|
92 |
+ |
|
93 |
+ describe "and the mode is last event in" do |
|
94 |
+ |
|
95 |
+ before { agent.options['mode'] = 'Last event in' } |
|
96 |
+ |
|
97 |
+ it "stores the last event in memory" do |
|
98 |
+ agent.receive incoming_events |
|
99 |
+ expect(agent.memory['last_event'][key]).to equal(value) |
|
100 |
+ end |
|
101 |
+ |
|
102 |
+ describe "but the casing is wrong" do |
|
103 |
+ before { agent.options['mode'] = 'LAST EVENT IN' } |
|
104 |
+ |
|
105 |
+ it "stores the last event in memory" do |
|
106 |
+ agent.receive incoming_events |
|
107 |
+ expect(agent.memory['last_event'][key]).to equal(value) |
|
108 |
+ end |
|
109 |
+ end |
|
110 |
+ |
|
111 |
+ end |
|
112 |
+ |
|
113 |
+ describe "but the mode is merge" do |
|
114 |
+ |
|
115 |
+ let(:second_key) { SecureRandom.uuid } |
|
116 |
+ let(:second_value) { SecureRandom.uuid } |
|
117 |
+ |
|
118 |
+ before { agent.options['mode'] = 'Merge events' } |
|
119 |
+ |
|
120 |
+ let(:incoming_events) do |
|
121 |
+ last_payload = { key => value } |
|
122 |
+ [Struct.new(:payload).new( { key => SecureRandom.uuid, second_key => second_value } ), |
|
123 |
+ Struct.new(:payload).new(last_payload)] |
|
124 |
+ end |
|
125 |
+ |
|
126 |
+ it "should merge all of the events passed to it" do |
|
127 |
+ agent.receive incoming_events |
|
128 |
+ expect(agent.memory['last_event'][key]).to equal(value) |
|
129 |
+ expect(agent.memory['last_event'][second_key]).to equal(second_value) |
|
130 |
+ end |
|
131 |
+ |
|
132 |
+ describe "but the casing on the mode is wrong" do |
|
133 |
+ |
|
134 |
+ before { agent.options['mode'] = 'MERGE EVENTS' } |
|
135 |
+ |
|
136 |
+ it "should merge all of the events passed to it" do |
|
137 |
+ agent.receive incoming_events |
|
138 |
+ expect(agent.memory['last_event'][key]).to equal(value) |
|
139 |
+ expect(agent.memory['last_event'][second_key]).to equal(second_value) |
|
140 |
+ end |
|
141 |
+ |
|
142 |
+ end |
|
143 |
+ |
|
144 |
+ end |
|
145 |
+ |
|
146 |
+ describe "but the mode is anything else" do |
|
147 |
+ |
|
148 |
+ before { agent.options['mode'] = SecureRandom.uuid } |
|
149 |
+ |
|
150 |
+ let(:incoming_events) do |
|
151 |
+ last_payload = { key => value } |
|
152 |
+ [Struct.new(:payload).new(last_payload)] |
|
153 |
+ end |
|
154 |
+ |
|
155 |
+ it "should do nothing" do |
|
156 |
+ agent.receive incoming_events |
|
157 |
+ expect(agent.memory.keys.count).to equal(0) |
|
158 |
+ end |
|
159 |
+ |
|
160 |
+ end |
|
161 |
+ |
|
162 |
+ end |
|
163 |
+ |
|
164 |
+ describe "#count_limit" do |
|
165 |
+ it "should have a default of 1000" do |
|
166 |
+ agent.options['event_limit'] = nil |
|
167 |
+ expect(agent.send(:count_limit)).to eq(1000) |
|
168 |
+ |
|
169 |
+ agent.options['event_limit'] = '' |
|
170 |
+ expect(agent.send(:count_limit)).to eq(1000) |
|
171 |
+ |
|
172 |
+ agent.options['event_limit'] = ' ' |
|
173 |
+ expect(agent.send(:count_limit)).to eq(1000) |
|
174 |
+ end |
|
175 |
+ |
|
176 |
+ it "should convert string count limits to integers" do |
|
177 |
+ agent.options['event_limit'] = '1' |
|
178 |
+ expect(agent.send(:count_limit)).to eq(1) |
|
179 |
+ |
|
180 |
+ agent.options['event_limit'] = '2' |
|
181 |
+ expect(agent.send(:count_limit)).to eq(2) |
|
182 |
+ |
|
183 |
+ agent.options['event_limit'] = 3 |
|
184 |
+ expect(agent.send(:count_limit)).to eq(3) |
|
185 |
+ end |
|
186 |
+ |
|
187 |
+ it "should default to 1000 with invalid values" do |
|
188 |
+ agent.options['event_limit'] = SecureRandom.uuid |
|
189 |
+ expect(agent.send(:count_limit)).to eq(1000) |
|
190 |
+ |
|
191 |
+ agent.options['event_limit'] = 'John Galt' |
|
192 |
+ expect(agent.send(:count_limit)).to eq(1000) |
|
193 |
+ end |
|
194 |
+ |
|
195 |
+ it "should not allow event limits above 1000" do |
|
196 |
+ agent.options['event_limit'] = '1001' |
|
197 |
+ expect(agent.send(:count_limit)).to eq(1000) |
|
198 |
+ |
|
199 |
+ agent.options['event_limit'] = '5000' |
|
200 |
+ expect(agent.send(:count_limit)).to eq(1000) |
|
201 |
+ end |
|
202 |
+ end |
|
203 |
+ |
|
204 |
+ describe "#receive_web_request?" do |
|
205 |
+ |
|
206 |
+ let(:secret) { SecureRandom.uuid } |
|
207 |
+ |
|
208 |
+ let(:params) { { 'secret' => secret } } |
|
209 |
+ |
|
210 |
+ let(:method) { nil } |
|
211 |
+ let(:format) { nil } |
|
212 |
+ |
|
213 |
+ let(:mime_type) { SecureRandom.uuid } |
|
214 |
+ let(:content) { "The key is {{#{key}}}." } |
|
215 |
+ |
|
216 |
+ let(:key) { SecureRandom.uuid } |
|
217 |
+ let(:value) { SecureRandom.uuid } |
|
218 |
+ |
|
219 |
+ before do |
|
220 |
+ agent.options['secret'] = secret |
|
221 |
+ agent.options['mime_type'] = mime_type |
|
222 |
+ agent.options['content'] = content |
|
223 |
+ agent.memory['last_event'] = { key => value } |
|
224 |
+ agents(:bob_website_agent).events.destroy_all |
|
225 |
+ end |
|
226 |
+ |
|
227 |
+ describe "and the mode is last event in" do |
|
228 |
+ |
|
229 |
+ before { agent.options['mode'] = 'Last event in' } |
|
230 |
+ |
|
231 |
+ it "should render the results as a liquid template from the last event in" do |
|
232 |
+ result = agent.receive_web_request params, method, format |
|
233 |
+ |
|
234 |
+ expect(result[0]).to eq("The key is #{value}.") |
|
235 |
+ expect(result[1]).to eq(200) |
|
236 |
+ expect(result[2]).to eq(mime_type) |
|
237 |
+ end |
|
238 |
+ |
|
239 |
+ describe "but the casing is wrong" do |
|
240 |
+ before { agent.options['mode'] = 'last event in' } |
|
241 |
+ |
|
242 |
+ it "should render the results as a liquid template from the last event in" do |
|
243 |
+ result = agent.receive_web_request params, method, format |
|
244 |
+ |
|
245 |
+ expect(result[0]).to eq("The key is #{value}.") |
|
246 |
+ expect(result[1]).to eq(200) |
|
247 |
+ expect(result[2]).to eq(mime_type) |
|
248 |
+ end |
|
249 |
+ end |
|
250 |
+ |
|
251 |
+ end |
|
252 |
+ |
|
253 |
+ describe "and the mode is merge events" do |
|
254 |
+ |
|
255 |
+ before { agent.options['mode'] = 'Merge events' } |
|
256 |
+ |
|
257 |
+ it "should render the results as a liquid template from the last event in" do |
|
258 |
+ result = agent.receive_web_request params, method, format |
|
259 |
+ |
|
260 |
+ expect(result[0]).to eq("The key is #{value}.") |
|
261 |
+ expect(result[1]).to eq(200) |
|
262 |
+ expect(result[2]).to eq(mime_type) |
|
263 |
+ end |
|
264 |
+ |
|
265 |
+ end |
|
266 |
+ |
|
267 |
+ describe "and the mode is last X events" do |
|
268 |
+ |
|
269 |
+ before do |
|
270 |
+ agent.options['mode'] = 'Last X events' |
|
271 |
+ |
|
272 |
+ agents(:bob_website_agent).create_event payload: { |
|
273 |
+ "name" => "Dagny Taggart", |
|
274 |
+ "book" => "Atlas Shrugged" |
|
275 |
+ } |
|
276 |
+ agents(:bob_website_agent).create_event payload: { |
|
277 |
+ "name" => "John Galt", |
|
278 |
+ "book" => "Atlas Shrugged" |
|
279 |
+ } |
|
280 |
+ agents(:bob_website_agent).create_event payload: { |
|
281 |
+ "name" => "Howard Roark", |
|
282 |
+ "book" => "The Fountainhead" |
|
283 |
+ } |
|
284 |
+ |
|
285 |
+ agent.options['content'] = <<EOF |
|
286 |
+<table> |
|
287 |
+ {% for event in events %} |
|
288 |
+ <tr> |
|
289 |
+ <td>{{ event.name }}</td> |
|
290 |
+ <td>{{ event.book }}</td> |
|
291 |
+ </tr> |
|
292 |
+ {% endfor %} |
|
293 |
+</table> |
|
294 |
+EOF |
|
295 |
+ end |
|
296 |
+ |
|
297 |
+ it "should render the results as a liquid template from the last event in, limiting to 2" do |
|
298 |
+ agent.options['event_limit'] = 2 |
|
299 |
+ result = agent.receive_web_request params, method, format |
|
300 |
+ |
|
301 |
+ expect(result[0]).to eq <<EOF |
|
302 |
+<table> |
|
303 |
+ |
|
304 |
+ <tr> |
|
305 |
+ <td>Howard Roark</td> |
|
306 |
+ <td>The Fountainhead</td> |
|
307 |
+ </tr> |
|
308 |
+ |
|
309 |
+ <tr> |
|
310 |
+ <td>John Galt</td> |
|
311 |
+ <td>Atlas Shrugged</td> |
|
312 |
+ </tr> |
|
313 |
+ |
|
314 |
+</table> |
|
315 |
+EOF |
|
316 |
+ end |
|
317 |
+ |
|
318 |
+ it "should render the results as a liquid template from the last event in, limiting to 1" do |
|
319 |
+ agent.options['event_limit'] = 1 |
|
320 |
+ result = agent.receive_web_request params, method, format |
|
321 |
+ |
|
322 |
+ expect(result[0]).to eq <<EOF |
|
323 |
+<table> |
|
324 |
+ |
|
325 |
+ <tr> |
|
326 |
+ <td>Howard Roark</td> |
|
327 |
+ <td>The Fountainhead</td> |
|
328 |
+ </tr> |
|
329 |
+ |
|
330 |
+</table> |
|
331 |
+EOF |
|
332 |
+ end |
|
333 |
+ |
|
334 |
+ it "should render the results as a liquid template from the last event in, allowing no limit" do |
|
335 |
+ agent.options['event_limit'] = '' |
|
336 |
+ result = agent.receive_web_request params, method, format |
|
337 |
+ |
|
338 |
+ expect(result[0]).to eq <<EOF |
|
339 |
+<table> |
|
340 |
+ |
|
341 |
+ <tr> |
|
342 |
+ <td>Howard Roark</td> |
|
343 |
+ <td>The Fountainhead</td> |
|
344 |
+ </tr> |
|
345 |
+ |
|
346 |
+ <tr> |
|
347 |
+ <td>John Galt</td> |
|
348 |
+ <td>Atlas Shrugged</td> |
|
349 |
+ </tr> |
|
350 |
+ |
|
351 |
+ <tr> |
|
352 |
+ <td>Dagny Taggart</td> |
|
353 |
+ <td>Atlas Shrugged</td> |
|
354 |
+ </tr> |
|
355 |
+ |
|
356 |
+</table> |
|
357 |
+EOF |
|
358 |
+ end |
|
359 |
+ |
|
360 |
+ it "should allow the limiting by time, as well" do |
|
361 |
+ |
|
362 |
+ one_event = agent.received_events.select { |x| x.payload['name'] == 'John Galt' }.first |
|
363 |
+ one_event.created_at = 2.days.ago |
|
364 |
+ one_event.save! |
|
365 |
+ |
|
366 |
+ agent.options['event_limit'] = '1 day' |
|
367 |
+ result = agent.receive_web_request params, method, format |
|
368 |
+ |
|
369 |
+ expect(result[0]).to eq <<EOF |
|
370 |
+<table> |
|
371 |
+ |
|
372 |
+ <tr> |
|
373 |
+ <td>Howard Roark</td> |
|
374 |
+ <td>The Fountainhead</td> |
|
375 |
+ </tr> |
|
376 |
+ |
|
377 |
+ <tr> |
|
378 |
+ <td>Dagny Taggart</td> |
|
379 |
+ <td>Atlas Shrugged</td> |
|
380 |
+ </tr> |
|
381 |
+ |
|
382 |
+</table> |
|
383 |
+EOF |
|
384 |
+ end |
|
385 |
+ |
|
386 |
+ it "should not be case sensitive when limiting on time" do |
|
387 |
+ |
|
388 |
+ one_event = agent.received_events.select { |x| x.payload['name'] == 'John Galt' }.first |
|
389 |
+ one_event.created_at = 2.days.ago |
|
390 |
+ one_event.save! |
|
391 |
+ |
|
392 |
+ agent.options['event_limit'] = '1 DaY' |
|
393 |
+ result = agent.receive_web_request params, method, format |
|
394 |
+ |
|
395 |
+ expect(result[0]).to eq <<EOF |
|
396 |
+<table> |
|
397 |
+ |
|
398 |
+ <tr> |
|
399 |
+ <td>Howard Roark</td> |
|
400 |
+ <td>The Fountainhead</td> |
|
401 |
+ </tr> |
|
402 |
+ |
|
403 |
+ <tr> |
|
404 |
+ <td>Dagny Taggart</td> |
|
405 |
+ <td>Atlas Shrugged</td> |
|
406 |
+ </tr> |
|
407 |
+ |
|
408 |
+</table> |
|
409 |
+EOF |
|
410 |
+ end |
|
411 |
+ |
|
412 |
+ it "it should continue to work when the event limit is wrong" do |
|
413 |
+ agent.options['event_limit'] = 'five days' |
|
414 |
+ result = agent.receive_web_request params, method, format |
|
415 |
+ |
|
416 |
+ expect(result[0].include?("Howard Roark")).to eq(true) |
|
417 |
+ expect(result[0].include?("Dagny Taggart")).to eq(true) |
|
418 |
+ expect(result[0].include?("John Galt")).to eq(true) |
|
419 |
+ |
|
420 |
+ agent.options['event_limit'] = '5 quibblequarks' |
|
421 |
+ result = agent.receive_web_request params, method, format |
|
422 |
+ |
|
423 |
+ expect(result[0].include?("Howard Roark")).to eq(true) |
|
424 |
+ expect(result[0].include?("Dagny Taggart")).to eq(true) |
|
425 |
+ expect(result[0].include?("John Galt")).to eq(true) |
|
426 |
+ end |
|
427 |
+ |
|
428 |
+ describe "but the mode was set to last X events with the wrong casing" do |
|
429 |
+ |
|
430 |
+ before { agent.options['mode'] = 'LAST X EVENTS' } |
|
431 |
+ |
|
432 |
+ it "should still work as last x events" do |
|
433 |
+ result = agent.receive_web_request params, method, format |
|
434 |
+ expect(result[0].include?("Howard Roark")).to eq(true) |
|
435 |
+ expect(result[0].include?("Dagny Taggart")).to eq(true) |
|
436 |
+ expect(result[0].include?("John Galt")).to eq(true) |
|
437 |
+ end |
|
438 |
+ |
|
439 |
+ end |
|
440 |
+ |
|
441 |
+ end |
|
442 |
+ |
|
443 |
+ describe "but the secret provided does not match" do |
|
444 |
+ before { params['secret'] = SecureRandom.uuid } |
|
445 |
+ |
|
446 |
+ it "should return a 401 response" do |
|
447 |
+ result = agent.receive_web_request params, method, format |
|
448 |
+ |
|
449 |
+ expect(result[0]).to eq("Not Authorized") |
|
450 |
+ expect(result[1]).to eq(401) |
|
451 |
+ end |
|
452 |
+ |
|
453 |
+ it "should return a 401 json response if the format is json" do |
|
454 |
+ result = agent.receive_web_request params, method, 'json' |
|
455 |
+ |
|
456 |
+ expect(result[0][:error]).to eq("Not Authorized") |
|
457 |
+ expect(result[1]).to eq(401) |
|
458 |
+ end |
|
459 |
+ end |
|
460 |
+ end |
|
461 |
+end |